using Microsoft.EntityFrameworkCore;
using Medical.Domain;

namespace Medical.Infrastructrue
{
    /// <summary>
    /// 医疗系统数据库上下文
    /// </summary>
    public class Mycontext : DbContext
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="options">数据库上下文配置选项</param>
        public Mycontext(DbContextOptions options) : base(options)
        {
        }

        /// <summary>
        /// 受保护的构造函数（用于设计时）
        /// </summary>
        protected Mycontext()
        {
        }

        /// <summary>
        /// 挂号单模板
        /// </summary>
        public DbSet<RegistrationSlipTemplate> RegistrationSlipTemplates { get; set; }
        /// <summary>
        /// 门诊单模板
        /// </summary>
        public DbSet<OutpatientOrderTemplate> OutpatientOrderTemplates { get; set; }
        /// <summary>
        /// 住院单模板
        /// </summary>
        public DbSet<InpatientOrderTemplate> InpatientOrderTemplates { get; set; }
        /// <summary>
        /// 科室
        /// </summary>
        public DbSet<Department> Departments { get; set; }
        /// <summary>
        /// 疾病
        /// </summary>
        public DbSet<Disease> Diseases { get; set; }
        /// <summary>
        /// 员工
        /// </summary>
        public DbSet<Employee> Employees { get; set; }
        /// <summary>
        /// 角色
        /// </summary>
        public DbSet<Role> Roles { get; set; }
        /// <summary>
        /// 权限
        /// </summary>
        public DbSet<Permission> Permissions { get; set; }
        /// <summary>
        /// 角色权限
        /// </summary>
        public DbSet<RolePermission> RolePermissions { get; set; }
        /// <summary>
        /// 员工角色
        /// </summary>
        public DbSet<EmployeeRole> EmployeeRoles { get; set; }
        /// <summary>
        /// 用户
        /// </summary>
        public DbSet<User> Users { get; set; }
        /// <summary>
        /// 用户角色
        /// </summary>
        public DbSet<UserRole> UserRoles { get; set; }

        /// <summary>
        /// 配置实体模型和关系
        /// </summary>
        /// <param name="modelBuilder">模型构建器</param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 配置所有实体的主键（显式配置以确保EF Core正确识别）
            modelBuilder.Entity<RegistrationSlipTemplate>().HasKey(e => e.TemplateId);
            modelBuilder.Entity<OutpatientOrderTemplate>().HasKey(e => e.TemplateId);
            modelBuilder.Entity<InpatientOrderTemplate>().HasKey(e => e.TemplateId);
            modelBuilder.Entity<Department>().HasKey(e => e.DepartmentId);
            modelBuilder.Entity<Disease>().HasKey(e => e.DiseaseId);
            modelBuilder.Entity<Employee>().HasKey(e => e.EmployeeId);
            modelBuilder.Entity<Role>().HasKey(e => e.RoleId);
            modelBuilder.Entity<Permission>().HasKey(e => e.PermissionId);
            modelBuilder.Entity<RolePermission>().HasKey(e => e.RolePermissionId);
            modelBuilder.Entity<EmployeeRole>().HasKey(e => e.EmployeeRoleId);
            modelBuilder.Entity<User>().HasKey(e => e.UserId);
            modelBuilder.Entity<UserRole>().HasKey(e => e.UserRoleId);
        }
    }
}
